package com.mialab.servlet_demo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/RequestHeadInfoServlet")
public class RequestHeadInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public RequestHeadInfoServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<h3>请求头信息<h3>");
		// 获取请求消息中所有头字段
		Enumeration headerNames = request.getHeaderNames();
		// 使用循环遍历所有请求头，并通过getHeader()方法获取一个指定名称的头字段
		while (headerNames.hasMoreElements()) {
			String headerName = (String) headerNames.nextElement();
			out.print(headerName + " : " + request.getHeader(headerName) + "<br>");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
